package com.hyc.myvideo.ui.fragment.column;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import butterknife.BindView;
import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.ColumnAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.Column;
import com.hyc.myvideo.ui.activity.column.ColumnActivity;
import com.hyc.myvideo.ui.base.BaseFragment;
import java.util.ArrayList;
import java.util.List;
import timber.log.Timber;

/**
 * 名师专栏
 * Created by dfeads on 2017/7/11.
 */

public class TeacherFragment extends BaseFragment {

  @BindView(R.id.fragment_teacher_pull_refresh) PullToRefreshListView mPullRefresh;
  private List<Column> columnList;
  private ColumnAdapter adapter;
  private int page = 1;
  private boolean isRefresh = true;

  @Override protected int getLayoutId() {
    return R.layout.fragment_teacher;
  }

  @Override protected void initView() {
    columnList = new ArrayList<>();
    adapter = new ColumnAdapter(getContext(), columnList);
    mPullRefresh.setAdapter(adapter);
    //专栏类型 （1=名师机构,2=课程）
    api.getColumnList("1", page);

    mPullRefresh.setMode(PullToRefreshBase.Mode.BOTH);
    mPullRefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
      @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        isRefresh = true;
        page = 1;
        api.getColumnList("1", page);
      }

      @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        isRefresh = false;
        page++;
        api.getColumnList("1", page);
      }
    });

    mPullRefresh.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        Timber.e("========onItemClick=======" + position);
        Column column = columnList.get(position - 1);
        startActivity(
            new Intent(getContext(), ColumnActivity.class).putExtra("id", column.getColumn_id()));
      }
    });
  }

  @Override protected void initData() {
  }

  @Override protected void onNexts(String result, String method) {
    BaseResult<List<Column>> columnB =
        JSONObject.parseObject(result, new TypeReference<BaseResult<List<Column>>>() {
        });
    if (columnB.isCode()) {
      if (isRefresh) {
        columnList.clear();
      }
      columnList.addAll(columnB.getResult());
      adapter.updateListView(columnList);
    } else {
      Toaster.show(columnB.getMsg());
    }
    if (mPullRefresh.isRefreshing()) {
      mPullRefresh.onRefreshComplete();
    }
  }

  @Override protected void onError() {
    if (mPullRefresh.isRefreshing()) {
      mPullRefresh.onRefreshComplete();
    }
  }
}
